<?php

namespace app\system\admin;

use app\common\model\User as UserModel;
use think\response\Json;
use think\helper;

/**
 * 用户
 * @package app\system\admin
 */
class Consumer extends Admin
{
    protected $hisiTable = 'User';
    protected $hisiModel = 'User';

    /**
     * 用户管理
     * @return string|Json|null
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $where = $this->getArr($where);
            $rs = UserModel::where($where)->page($page)->limit($limit)->order('id','desc')->select();
            $count = UserModel::where($where)->count('id');
            return $this->layuiJson($rs , $count);
        }
        return $this->fetch();
    }

    /**
     * 添加用户
     * @return string|null
     */
    public function add($id = 0)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Consumer');
            if($result !== true) {
                return $this->error($result);
            }
            if ($data['username'] && UserModel::where(['username'=>$data['username']])->count()){
                return $this->error('用户名已存在');
            }
            if ($data['email'] && UserModel::where(['email'=>$data['email']])->count()){
                return $this->error('邮箱已存在');
            }
            if ($data['mobile'] && UserModel::where(['mobile'=>$data['mobile']])->count()){
                return $this->error('电话已经存在');
            }
            $data['password'] = password_hash(md5($data['password']), PASSWORD_DEFAULT);
            if (!UserModel::create($data)) {
                return $this->error('添加失败');
            }
            return $this->success('添加成功');
        }
        $this->assign('isUpdate',0);
        return $this->fetch();
    }

    /**
     * 修改用户
     * @param int $id
     * @return string|null
     */
    public function edit($id = 0)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($data['password']) {
                $data['password'] = md5($data['password']);
            }

            if ($data['password']) {
                $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
            } else {
                unset($data['password']);
            }
            if (!UserModel::update($data)) {
                return $this->error('修改失败');
            }
            return $this->success('修改成功');
        }

        $row = UserModel::where('id', '=', $id)->field('id,username,nick,email,mobile,status,portrait')->find()->toArray();
        $this->assign('isUpdate',1);
        $this->assign('formData', $row);
        return $this->fetch('add');
    }

    /**
     * 用户管理
     * @return Json|string
     */
    public function export($q = '')
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $where = $this->getArr($where);
            $rs = UserModel::where($where)
                ->field('id,username,nick,mobile,email,status,last_login_ip,last_login_time,ctime,mtime')
                ->order('id','desc')
                ->select()
                ->toArray();
            $count = UserModel::where($where)->count('id');
            $headers = ['用户ID','用户名','昵称','电话','邮箱','状态','最后登录ip','最后登录时间','创建时间','修改时间'];
            return json(['data'=>$rs,'count'=>$count,'code'=>0,'headers'=>$headers]);
        }
        return $this->fetch();
    }

    /**
     * @param array $where
     * @return array
     */
    public function getArr(array $where)
    {
        $uid = $this->request->param('uid/d');
        $username = $this->request->param('username/s');
        $nick = $this->request->param('nick/s');
        $phone = $this->request->param('phone/s');
        $status = $this->request->param('status/d');
        if ($uid) {
            $where[] = ['id', '=', $uid];
        }
        if ($username) {
            $where[] = ['username', 'like', '%' . $username . '%'];
        }
        if ($nick) {
            $where[] = ['nick', 'like', '%' . $nick . '%'];
        }
        if ($phone) {
            $where[] = ['mobile', 'like', '%' . $phone . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }
        return $where;
    }

}
